package client.ui;

import java.util.List;

import phasebook.client.msgs.MovementInfo;
import client.Controller;
import client.Controller.ResponseHandler;
import client.api.CombinedResponse;

import com.google.gwt.i18n.shared.DateTimeFormat;
import com.google.gwt.i18n.shared.DateTimeFormat.PredefinedFormat;
import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.HTMLPane;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.ComboBoxItem;
import com.smartgwt.client.widgets.form.fields.FormItem;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.SectionStackSection;
import com.smartgwt.client.widgets.layout.VLayout;

public class LotterySection extends SectionStackSection implements ResponseHandler{
	private VLayout layout = new VLayout();

	public LotterySection(final Controller controller) {
		super("Lottery");
		super.setExpanded(true);
		super.setResizeable(true);
		
		final TextItem betValueItem = new TextItem();
		betValueItem.setWidth("40");
		betValueItem.setTitle("Value");
		betValueItem.setValue("10");
		
		final TextItem resultItem = new TextItem();
		resultItem.setWidth("40");
		resultItem.setTitle("Result");
		resultItem.setValue("50");

		IButton betButton = new IButton("Bet");
		betButton.setWidth("25%");
		betButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				controller.placeBet(
					Double.parseDouble(betValueItem.getValueAsString()), 
					Integer.parseInt(resultItem.getValueAsString()));
			}
		});
		
		DynamicForm form = new DynamicForm();
		form.setWidth100();
		form.setHeight(30);
		form.setNumCols(4);
		form.setFields(resultItem,betValueItem);
		
		HLayout bettingTool = new HLayout();
		bettingTool.setHeight("1%");
		bettingTool.addMember( form);
		bettingTool.addMember(betButton);
		 
		
		layout.setHeight("25%");
		layout.setOverflow(Overflow.AUTO);
		super.addItem(bettingTool);
		super.addItem(layout);
		controller.addResponseHandler(this);
	}
	private HTMLPane createNews(MovementInfo info){
		HTMLPane news = new HTMLPane();
		news.setWidth100();
		news.setHeight("1%");
		news.setOverflow(Overflow.VISIBLE);
		news.setContents(
				"<div style=\"color:"+(info.getAmount()<=0?"red":"green")+";\">" +
				"<b>"+info.getAmount() +" L€</b> : "+info.getDescription()+"</div><div style=\"text-align: right\">"+
				DateTimeFormat.getFormat(PredefinedFormat.RFC_2822).format(info.getTimestamp())+
				"</div>");
		return news;
	}

	@Override
	public void onResponse(CombinedResponse response) {
		List<MovementInfo> movements = response.getMovements();
		if (movements!=null){
			Canvas[] items = new Canvas[movements.size()];
			for(int i=0;i<movements.size();i++)
				items[movements.size()-i-1] = createNews(movements.get(i));
			
			layout.setMembers(items);
			HTMLPane emptySpace = new HTMLPane();
			emptySpace.setWidth100();
			emptySpace.setHeight100();
			emptySpace.setOverflow(Overflow.CLIP_V);
			layout.addMember(emptySpace);
		}
	}
}
